//
//  FPSetPasswordController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/4/10.
//

#import "FPSetPasswordController.h"

@interface FPSetPasswordController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *tipLab1;
@property (weak, nonatomic) IBOutlet UILabel *tipLab2;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField1;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField2;

@property (weak, nonatomic) IBOutlet UILabel *errorTipLab1;
@property (weak, nonatomic) IBOutlet UILabel *errorTipLab2;
@property (weak, nonatomic) IBOutlet UIView *lineView1;
@property (weak, nonatomic) IBOutlet UIView *lineView2;

@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (weak, nonatomic) IBOutlet UIImageView *showPwdImgView1;
@property (weak, nonatomic) IBOutlet UIImageView *showPwdImgView2;

@property (nonatomic , assign) BOOL showPwdOpen1;
@property (nonatomic , assign) BOOL showPwdOpen2;

@property (nonatomic , strong) NSString *paramPwd1;
@property (nonatomic , strong) NSString *paramPwd2;

@end

@implementation FPSetPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.showPwdOpen1 = NO;
    self.showPwdOpen2 = NO;
    
    // 删除控制器
    NSMutableArray *marr = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
    
    for (UIViewController *vc in marr) {
    
        if ([vc isKindOfClass:NSClassFromString(@"FPLoginController")]) {
            
            [marr removeObject:vc];
            break;
        }
    }
    
    for (UIViewController *vc in marr) {
    
        if ([vc isKindOfClass:NSClassFromString(@"FPForgetPasswordController")]) {
            
            [marr removeObject:vc];
            break;
        }
    }
    
    self.navigationController.viewControllers = marr;
}


- (void)initUI {
    
    [self setTitle:@"" style:(XYCustomNavigationStyleWhite) hiddenBackBtn:NO];
    
    self.errorTipLab1.hidden = YES;
    self.errorTipLab2.hidden = YES;
    
    self.errorTipLab1.textColor = UIColor.redColor;
    self.errorTipLab2.textColor = UIColor.redColor;
    
    self.lineView1.backgroundColor = [[UIColor hexColor:@"#CCCCCC"] colorWithAlphaComponent:.5f];
    self.lineView2.backgroundColor = [[UIColor hexColor:@"#CCCCCC"] colorWithAlphaComponent:.5f];
    
    self.tipLab1.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.tipLab2.textColor = [UIColor hexColor:@"#6A6A6A"];
    
    self.confirmBtn.clipsToBounds = YES;
    self.confirmBtn.layer.cornerRadius = 22.f;
    self.confirmBtn.backgroundColor = MAIN_BLUE_COLOR;
    
    [self.showPwdImgView1 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPasswordAction:)]];
    [self.showPwdImgView2 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPasswordAction:)]];
    
    self.pwdTextField1.delegate = self;
    self.pwdTextField2.delegate = self;
    
    self.pwdTextField1.secureTextEntry = YES;
    self.pwdTextField2.secureTextEntry = YES;
}


- (void)showPasswordAction:(UITapGestureRecognizer *)sender {
    
    FeedBackGeneratorAction
    [self.view endEditing:YES];
    
    if ([sender.view isEqual:self.showPwdImgView1]) {
     
        self.showPwdOpen1 = !self.showPwdOpen1;
        self.pwdTextField1.secureTextEntry = !self.showPwdOpen1;
        
        if (self.showPwdOpen1) {
         
            self.showPwdImgView1.image = IMG(@"icon_pwd_open");
        }
        else {
         
            self.showPwdImgView1.image = IMG(@"icon_pwd_close");
        }
    }
    else if ([sender.view isEqual:self.showPwdImgView2]) {
        
        self.showPwdOpen2 = !self.showPwdOpen2;
        self.pwdTextField2.secureTextEntry = !self.showPwdOpen2;
        
        if (self.showPwdOpen2) {
         
            self.showPwdImgView2.image = IMG(@"icon_pwd_open");
        }
        else {
         
            self.showPwdImgView2.image = IMG(@"icon_pwd_close");
        }
    }
}

- (BOOL)isValidPassword:(NSString *)text {
    
    if (text.length < 8) return NO;

    // 只允许数字、字母、符号
    NSString *allowedPattern = @"^[A-Za-z0-9\\p{P}\\p{S}]+$";
    NSRegularExpression *allowedRegex = [NSRegularExpression regularExpressionWithPattern:allowedPattern options:0 error:nil];
    NSUInteger match = [allowedRegex numberOfMatchesInString:text options:0 range:NSMakeRange(0, text.length)];
    if (match == 0) return NO;

    // 判断包含的类型
    BOOL hasDigit = [text rangeOfString:@"\\d" options:NSRegularExpressionSearch].location != NSNotFound;
    BOOL hasLetter = [text rangeOfString:@"[A-Za-z]" options:NSRegularExpressionSearch].location != NSNotFound;
    BOOL hasSymbol = [text rangeOfString:@"[^A-Za-z0-9]" options:NSRegularExpressionSearch].location != NSNotFound;

    int typeCount = (hasDigit ? 1 : 0) + (hasLetter ? 1 : 0) + (hasSymbol ? 1 : 0);
    return typeCount >= 2;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if ([textField isEqual:self.pwdTextField1]) {
    
        self.paramPwd1 = textField.text;;
    }
    else if ([textField isEqual:self.pwdTextField2]) {
        
        self.paramPwd2 = textField.text;;
    }
}


- (IBAction)confirmAction:(UIButton *)sender {
    
    if (![self isValidPassword:self.paramPwd1]) {
        
        self.errorTipLab1.hidden = NO;
        [self.pwdTextField1 shake];
        self.errorTipLab1.text = @"密码不符合规范";
        return;
    }
    else {
        
        self.errorTipLab1.hidden = YES;
    }
    
    if (![self.paramPwd1 isEqualToString:self.paramPwd2]) {
        
        self.errorTipLab2.hidden = NO;
        [self.pwdTextField2 shake];
        self.errorTipLab2.text = @"密码输入不一致";
        return;
    }
    else {
        
        self.errorTipLab2.hidden = YES;
    }
    
    // 设置密码接口
    // ...
    
    UIWindow *keyWindow = [UIApplication sharedApplication].windows.firstObject;
    keyWindow.rootViewController = [[FPMainTabBarController alloc] initWithContext:nil sourcetype:@"userInfo.platformType"];
}




@end
